<?php


namespace App\Http\Controllers;


use App\Http\Resources\AssetResource;
use App\Http\Resources\CommonModelResource;

trait pagniateable
{

    protected function paginate($query)
    {
        $result = new CommonModelResource(collect());
        $itemsPerPage = request()->get('itemsPerPage', 10);
        $page = request()->get('page');
        $sortBy = request()->get('sortBy');
        $queryString = request()->get('query');

        if (!empty($queryString)) {
            $query->fuzzyFilter($queryString);
        }
        if (!empty($sortBy)) {
            $orderByColumn = $sortBy[0];
            $sortDesc = request()->get('sortDesc');
            $sortType = $sortDesc[0] ? 'desc' : 'asc';
            $query->orderBy($orderByColumn, $sortType);
        } else {
            $query->orderBy('id', 'desc');
        }
        $result = new CommonModelResource($query->paginate($itemsPerPage));
        return $result;
    }
}
